import math
import pandas as pd
import numpy as np

def cumror(netlist):
    '''
    :param netlist:净值曲线
    :return: 累计收益
    '''
    return math.pow(netlist[-1] / netlist[0], 1) - 1

# 计算对数收益率
def price_to_rev(netlist):
    r = list()
    for i in range(1, len(netlist)):
        r.append(math.log(netlist[i] / netlist[i - 1]))
    return r


def volatility(netlist):
    r = price_to_rev(netlist)
    return np.std(r) * math.pow(252, 0.5)


def maxretrace(netlist):
    '''
    :param list:netlist
    :return: 最大历史回撤
    '''
    Max = 0.0001
    for i in range(len(netlist)):
        if 1 - netlist[i] / max(netlist[:i + 1]) > Max:
            Max = 1 - netlist[i] / max(netlist[:i + 1])
    return Max


def yearsharpRatio(netlist):
    row = []
    for i in range(1, len(netlist)):
        row.append(math.log(netlist[i] / netlist[i - 1]))
    print(np.mean(row))
    return (np.mean(row)*252 -0.03 )/ (np.std(row) * math.pow(252, 0.5))
    #return (cumror(netlist)-0.03) / (np.std(row) * math.pow(252, 0.5))


def avg_yearly_ret(netlist):
    r = price_to_rev(netlist)
    return np.mean(r) * 252


if __name__ == '__main__':
    df = pd.read_csv("股票型过去一年.csv")
    print(df)
    netlist = df["index_level"].to_list()
    print(cumror(netlist))
    print(avg_yearly_ret(netlist))
    print(volatility(netlist))
    print(maxretrace(netlist))
    print(yearsharpRatio(netlist))